package cn.lili.modules.goods.client;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.cache.CachePrefix;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.constant.ServiceConstant;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
import cn.lili.modules.goods.fallback.GoodsSkuFallback;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: ftyy
 * @date: 2022-01-12 10:52
 * @description: goodsSku Client
 */
@FeignClient(path = "${server.servlet.context-path}", name = ServiceConstant.GOODS_SERVICE, contextId = "goods-sku", fallback = GoodsSkuFallback.class)
public interface GoodsSkuClient {

    /**
     * 获取商品SKU库存缓存ID
     *
     * @param orderItem 订单项
     * @return 商品SKU缓存ID
     */
    @GetMapping("/feign/goods/order-item/getStockCacheKey")
    static String getStockCacheKey(@RequestParam OrderItem orderItem) {
        if (CharSequenceUtil.isNotEmpty(orderItem.getSupplierSkuId())) {
            return CachePrefix.SKU_STOCK.getPrefix() + orderItem.getSupplierSkuId();
        }
        return CachePrefix.SKU_STOCK.getPrefix() + orderItem.getSkuId();
    }

    /**
     * 获取商品SKU库存缓存ID
     *
     * @param goodsSku 商品SKU
     * @return 商品SKU缓存ID
     */
    @GetMapping("/feign/goods/goods-sku/getStockCacheKey")
    static String getStockCacheKey(@RequestParam GoodsSku goodsSku) {
        if (StringUtils.isNotEmpty(goodsSku.getSupplierSkuId())) {
            return CachePrefix.SKU_STOCK.getPrefix() + goodsSku.getSupplierSkuId();
        }
        return CachePrefix.SKU_STOCK.getPrefix() + goodsSku.getId();
    }

    /**
     * 从redis缓存中获取商品SKU信息
     *
     * @param id SkuId
     * @return 商品SKU信息
     */
    @GetMapping("/feign/goods/goods-sku/getGoodsSkuByIdFromCache")
    GoodsSku getGoodsSkuByIdFromCache(@RequestParam String id);

    /**
     * 批量从redis中获取商品SKU信息
     *
     * @param ids SkuId集合
     * @return 商品SKU信息集合
     */
    @PostMapping("/feign/goods/goods-sku/getGoodsSkuByIdFromCache")
    List<GoodsSku> getGoodsSkuByIdFromCache(@RequestBody List<String> ids);

    /**
     * 获取商品sku库存
     *
     * @param skuId 商品skuId
     * @return 库存数量
     */
    @GetMapping("/feign/goods/goods-sku/getStock")
    Integer getStock(@RequestParam String skuId);


    /**
     * 更新SKU评价数量
     *
     * @param skuId SKUId
     */
    @PutMapping("/feign/goods/goods-sku/updateGoodsSkuCommentNum")
    void updateGoodsSkuCommentNum(@RequestParam String skuId);

    @PutMapping("/feign/goods/goods-sku/updateBatchById")
    void updateBatchById(@RequestBody List<GoodsSku> goodsSkus);

    /**
     * 分页获取商品SKU
     *
     * @param searchParams
     * @return
     */
    @PutMapping("/feign/goods/goods-sku/getGoodsSkuByPage")
    IPage<GoodsSku> getGoodsSkuByPage(@RequestBody GoodsSearchParams searchParams);


    /**
     * 分页获取商品SKU
     *
     * @param searchParams
     * @return
     */
    @PutMapping("/feign/goods/goods-sku/getGoodsSkuCount")
    Long getGoodsSkuCount(@RequestBody GoodsSearchParams searchParams);

    /**
     * 分页获取商品SKU
     *
     * @param searchParams
     * @return
     */
    @PutMapping("/feign/goods/goods-sku/getGoodsSkuDTOByPage")
    Page<GoodsSkuDTO> getGoodsSkuDTOByPage(@RequestBody GoodsSearchParams searchParams);

    @GetMapping("/feign/goods/getGoodsSkuByGoodsIdList")
    List<GoodsSku> getGoodsSkuByGoodsIdList(@RequestParam("goodsIdList") List<String> goodsIdList);

    @GetMapping("/feign/goods/getGoodsSkuById")
    GoodsSku getGoodsSkuById(@RequestParam("skuId") String skuId);

    @GetMapping("/feign/goods/updateGoodsSkuCache")
    void updateGoodsSkuCache(@RequestParam List<String> goodsIds);

    @ApiOperation(value = "根据店铺获取店铺的套餐商品规格列表")
    @GetMapping("/feign/goods/getGoodsSkuByStoreIds")
    List<GoodsSku> getGoodsSkuByStoreIds(@RequestParam List<String> storeIds);
}
